var ctx;

function setCtx(ctx){
	this.ctx = ctx;
}
function mouseOverImage(target){
	$(target).attr("src","/manage/images/removemenu_red.png");
}
function mouseOutImage(target){
	$(target).attr("src","/manage/images/removemenu_green.png");
     }
		var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
		//保存
    	function saveOrUpdateUserShop () {
    		//通过过滤器获取有三已选中的三级节点
    		var treeObj = $.fn.zTree.getZTreeObj("treeDemo");
	       	 var nodes = treeObj.getNodesByFilter(filter);
    		var selectUserShop = [];
    		if(nodes.length > 0){
    			for(var i = 0 ; i < nodes.length ; i ++){
    				selectUserShop.push(nodes[i].id);
        		}
    		}
    		
    		//无数据则造一条假数据,以免报错
    		if(selectUserShop.length == 0){
    			selectUserShop.push(0);
    		}
    		
    		//将所选小区id数组传给父页面
    		 parent.$('#selectedShopId').val(selectUserShop);
    	       //关闭窗口
    	     parent.layer.close(index);
    	}
		
		var curMenu = null, zTree_Menu = null;
		var setting = {
			view: {
				showLine: true,
				showIcon: false,
				selectedMulti: false,
				dblClickExpand: false,
				addDiyDom: addDiyDom,
				fontCss: setFontCss_ztree,
			},
			edit: {
				enable: false,
				showRemoveBtn: false,
				showRenameBtn: false
				},
			data: {
				keep: {
					parent:true,
					leaf:true
				},
				simpleData: {
					enable: true,
					idKey: "id",
					pIdKey: "pId",
					rootPid: null 
				},
			},
			callback: {
				/* beforeClick: beforeClick, */
				onClick : onClick,
				onAsyncSuccess: zTreeOnAsyncSuccess,
				beforeDrag: beforeDrag,
				onCheck : onCheck,
				removeShop:removeShop
			},check: {
		        enable: true
		    }
		};

  		var zNodes;
  		
  		//点击树节点触发时间
  		function onClick(event, treeId, treeNode, clickFlag) {  
  			
			var zTree = $.fn.zTree.getZTreeObj("treeDemo"),
			nodes = zTree.getSelectedNodes(),
			treeNode = nodes[0];
			
			loading(treeNode);
			choosedNode(treeNode);
  		}
  		
  		function loading(treeNode){
 		   if(treeNode.checked){
 			  zTree_Menu.checkNode(zTree_Menu.getNodeByParam("id", treeNode.id, null), false, true);

 		   }else{
 			  zTree_Menu.checkNode(zTree_Menu.getNodeByParam("id", treeNode.id, null), true, true);
 		   }
  		}
  		
  		//点击勾选/取消勾选触发事件
  		function onCheck(event, treeId, treeNode) {  
  			
			choosedNode(treeNode);

  		}	
  		
  		function choosedNode(treeNode){
			  if(treeNode.checked){
				  if(treeNode.level == 0){
					  if(treeNode.children.length > 0){
						  for(var i=0;i < treeNode.children.length;i++){
							  if(treeNode.children[i].children.length > 0){
								  for(var j=0;j < treeNode.children[i].children.length;j++){
									  if(treeNode.children[i].children[j].children.length > 0){
										  for(var k=0;k < treeNode.children[i].children[j].children.length;k++){
											  if(!treeNode.children[i].children[j].children[k].chkDisabled){
											  var html='<div id="shopId' + treeNode.children[i].children[j].children[k].id + '" style="margin-top:12px;margin-left:12px;"><div style="float:left;"><span>' + treeNode.name + '</span>';
											  html += '<span><img src="/manage/images/jianjiao.png" style="padding-left:4px;padding-right:4px"></span><span>' + treeNode.children[i].name + '</span>';
											  html += '<span><img src="/manage/images/jianjiao.png" style="padding-left:4px;padding-right:4px"></span><span>' + treeNode.children[i].children[j].name + '</span>';
											  html += '<span><img src="/manage/images/jianjiao.png" style="padding-left:4px;padding-right:4px"></span><span>' + treeNode.children[i].children[j].children[k].name + '</span>';
											  html += '</div><div style="float:right;"><span><img onmouseover="mouseOverImage(this)" onmouseout="mouseOutImage(this)"  onclick="removeShop('+treeNode.children[i].children[j].children[k].id+')" style="margin-right:12px;cursor:pointer;" src="/manage/images/removemenu_green.png"> </span>';
											  html += '</div><span><br/></span></div>';
											  $("#checkedUserShop").append(html);
											  }
										  }
									  }
								  }
							  }
						  }
					  }
					  
				  }else if(treeNode.level == 1){
					  if(treeNode.children.length > 0){
						  for(var i=0;i < treeNode.children.length;i++){
							  if(treeNode.children[i].children.length > 0){
								  for(var j=0;j < treeNode.children[i].children.length;j++){
									  if(!treeNode.children[i].children[j].chkDisabled){
									  var html = '<div id="shopId' + treeNode.children[i].children[j].id + '" style="margin-top:12px;margin-left:12px;"><div style="float:left;"><span>' + treeNode.getParentNode().name + '</span>';
									  html += '<span><img src="/manage/images/jianjiao.png" style="padding-left:4px;padding-right:4px"></span><span>' + treeNode.name + '</span>';
									  html += '<span><img src="/manage/images/jianjiao.png" style="padding-left:4px;padding-right:4px"></span><span id=' + treeNode.children[i].id + '>' + treeNode.children[i].name + '</span>';
									  html += '<span><img src="/manage/images/jianjiao.png" style="padding-left:4px;padding-right:4px"></span><span id=' + treeNode.children[i].children[j].id + '>' + treeNode.children[i].children[j].name + '</span>';
									  html += '</div><div style="float:right;"><span><img onmouseover="mouseOverImage(this)" onmouseout="mouseOutImage(this)"  onclick="removeShop('+treeNode.children[i].children[j].id+')" style="margin-right:12px;cursor:pointer;" src="/manage/images/removemenu_green.png"> </span>';
									  html += '</div><span><br/></span></div>';
									  $("#checkedUserShop").append(html);
									  }
								  }
							  }
						  }
					  }
				  }else if(treeNode.level == 2){
					  for(var i=0;i < treeNode.children.length;i++){
						  if(!treeNode.children[i].chkDisabled){
						  var html = '<div id="shopId' + treeNode.children[i].id + '" style="margin-top:12px;margin-left:12px;"><div style="float:left;"><span>' +  treeNode.getParentNode().getParentNode().name+ '</span>';
						  html += '<span><img src="/manage/images/jianjiao.png" style="padding-left:4px;padding-right:4px"></span><span>' +  treeNode.getParentNode().name+ '</span>';
						  html += '<span><img src="/manage/images/jianjiao.png" style="padding-left:4px;padding-right:4px"></span><span><span id='+ treeNode.id +'>' + treeNode.name + '</span>';
						  html += '<span><img src="/manage/images/jianjiao.png" style="padding-left:4px;padding-right:4px"></span><span><span id='+ treeNode.children[i].id +'>' + treeNode.children[i].name + '</span>';
						  html += '</div><div><span style="float:right;"><img onmouseover="mouseOverImage(this)" onmouseout="mouseOutImage(this)"  onclick="removeShop('+treeNode.children[i].id+')" style="margin-right:12px;cursor:pointer;" src="/manage/images/removemenu_green.png"> </span>';
						  html += '</div><span><br/></span></div>';
						  $("#checkedUserShop").append(html);
						  }
					  }
					  
				  }else if(treeNode.level == 3){
					  if(!treeNode.chkDisabled){
					  var html = '<div id="shopId' + treeNode.id + '" style="margin-top:12px;margin-left:12px;"><div style="float:left;"><span>' +  treeNode.getParentNode().getParentNode().getParentNode().name+ '</span>';
					  html += '<span><img src="/manage/images/jianjiao.png" style="padding-left:4px;padding-right:4px"></span><span>' +  treeNode.getParentNode().getParentNode().name+ '</span>';
					  html += '<span><img src="/manage/images/jianjiao.png" style="padding-left:4px;padding-right:4px"></span><span><span id='+ treeNode.getParentNode().id +'>' + treeNode.getParentNode().name + '</span>';
					  html += '<span><img src="/manage/images/jianjiao.png" style="padding-left:4px;padding-right:4px"></span><span><span id='+ treeNode.id +'>' + treeNode.name + '</span>';
					  html += '</div><div><span style="float:right;"><img onmouseover="mouseOverImage(this)" onmouseout="mouseOutImage(this)"  onclick="removeShop('+treeNode.id+')" style="margin-right:12px;cursor:pointer;" src="/manage/images/removemenu_green.png"> </span>';
					  html += '</div><span><br/></span></div>';
					  $("#checkedUserShop").append(html);
					  }
				  }
			  }else{
				  if(treeNode.level == 0){
					  if(treeNode.children.length > 0){
						  for(var i=0;i < treeNode.children.length;i++){
							  if(treeNode.children[i].children.length > 0){
								  for(var j=0;j < treeNode.children[i].children.length;j++){
									  if(treeNode.children[i].children[j].children.length > 0){
										  for(var k=0;k < treeNode.children[i].children[j].children.length;k++){
											  if(!treeNode.children[i].children[j].children[k].chkDisabled){
											  $("#shopId" + treeNode.children[i].children[j].children[k].id).remove();
											  }
										  }
								  }
							  }
							  
						  }
					  }
				  }	  
			  }else if(treeNode.level == 1){
					  for(var i=0;i < treeNode.children.length;i++){
						  if(treeNode.children[i].children.length > 0){
							  for(var j=0;j < treeNode.children[i].children.length;j++){
								  if(!treeNode.children[i].children[j].chkDisabled){
								  $("#shopId" + treeNode.children[i].children[j].id).remove();
								  }
							  }
						  }
					  }
			  }else if(treeNode.level == 2){
					  if(treeNode.children.length > 0){
						  for(var i=0;i < treeNode.children.length;i++){
							  if(!treeNode.children[i].chkDisabled){
							  $("#shopId" + treeNode.children[i].id).remove();
							  }
						  }
					  }
			  }else{
				  if(!treeNode.chkDisabled){
				  $("#shopId" + treeNode.id).remove();
				  }
			  }
				  
		}
			  
  		}
  		
 		function zTreeOnAsyncSuccess(event, treeId, treeNode, msg) {
 			zNodes = JSON.parse(msg);
 			layer.msg(msg, {icon: 5});
 		};
 			
		function addDiyDom(treeId, treeNode) {
			var spaceWidth = 5;
			var switchObj = $("#" + treeNode.tId + "_switch"),
			icoObj = $("#" + treeNode.tId + "_ico");
			switchObj.remove();
			icoObj.before(switchObj);

			if (treeNode.level > 1) {
				var spaceStr = "<span style='display: inline-block;width:" + (spaceWidth * treeNode.level)+ "px'></span>";
				switchObj.before(spaceStr);
			}
		}

		
		var treeNodes;
		 
		function onLoadZTree(){
			var roles = parent.$("#selectRoles").val();
			 
			   $.ajax({
			    async:true,//是否异步
			    cache:false,//是否使用缓存
			    type:'post',//请求方式：post
			    dataType:'json',//数据传输格式：json
			    url:ctx + "/user/treeData?roles=" + roles,//请求的action路径
			    error:function(){
			      //请求失败处理函数
			      layer.msg('请求失败!', {icon: 5});
			    },
			    success:function(data){
			    	if(!data.success){
			    		layer.msg(data.obj, {icon: 5});
			    		return;
			    	}
			    	
			      var t = $("#treeDemo");
					 $.fn.zTree.init(t, setting, eval(data.obj));
					 
					 //初始化树时默认选择第一个节点
						zTree_Menu = $.fn.zTree.getZTreeObj("treeDemo");
						var nodes = zTree_Menu.getSelectedNodes();
						curMenu = zTree_Menu.getNodes();
						
						
						//从后台传过来的当前商户绑定的店铺id集合
						var shopList = $("#shopList").val();
						var items;
						if(shopList != null && shopList != undefined && shopList != ""){
							items = eval("("+ shopList +")");
							//勾选店铺树，并将已选择的树在右边列表展示
							if(items.length > 0){
								for(var i = 0 ; i < items.length ; i ++){
									//zTree_Menu.checkNode(zTree_Menu.getNodeByParam("id", items[i].id, null), true, true);
									var node = zTree_Menu.getNodeByParam("id", items[i], null);
									
									if(node == null){
										continue;
									}
									
									//勾选当前节点
									node.checked = true;
									zTree_Menu.updateNode(node);
									//勾选三级节点
									node.getParentNode().checked = true;
									zTree_Menu.updateNode(node.getParentNode());
									//勾选二级节点
									node.getParentNode().getParentNode().checked = true;
									zTree_Menu.updateNode(node.getParentNode().getParentNode());
									//勾选一级节点
									node.getParentNode().getParentNode().getParentNode().checked = true;
									zTree_Menu.updateNode(node.getParentNode().getParentNode().getParentNode());
									var html = '<div id="shopId' + node.id + '" style="margin-top:12px;margin-left:12px;"><div style="float:left;"><span>' +  node.getParentNode().getParentNode().getParentNode().name+ '</span>';
									html += '<span><img src="/manage/images/jianjiao.png" style="padding-left:4px;padding-right:4px"></span><span>' +  node.getParentNode().getParentNode().name+ '</span>';
									html += '<span><img src="/manage/images/jianjiao.png" style="padding-left:4px;padding-right:4px"></span><span>' +  node.getParentNode().name+ '</span>';
									html += '<span><img src="/manage/images/jianjiao.png" style="padding-left:4px;padding-right:4px"></span><span><span id='+ node.id +'>' + node.name + '</span>';
									html += '</div><div><span style="float:right;"><img onmouseover="mouseOverImage(this)" onmouseout="mouseOutImage(this)"  onclick="removeShop('+node.id+')" style="margin-right:12px;cursor:pointer;" src="/manage/images/removemenu_green.png"> </span>';
									html += '</div><span><br/></span></div>';
									$("#checkedUserShop").append(html);
								}
							}
						}
						
						
						//展开所有
						zTree_Menu.expandAll(true);
						
						t.hover(function () {
							if (!t.hasClass("showIcon")) {
								t.addClass("showIcon"); 
							}
						}, function() {
							t.removeClass("showIcon");
						});
						
			    }
			  });
			  
			  
			}
		
		$(document).ready(function(){
			onLoadZTree();
		});
			
		
		//==========================搜索树begin===================================//
		
		/**
     * 展开树
     * @param treeId  
     */
    function expand_ztree(treeId){
        var treeObj = $.fn.zTree.getZTreeObj(treeId);
        treeObj.expandAll(true);
    }
     
    /**
     * 收起树：只展开根节点下的一级节点
     * @param treeId
     */
    function close_ztree(treeId){
        var treeObj = $.fn.zTree.getZTreeObj(treeId);
        var nodes = treeObj.transformToArray(treeObj.getNodes());
        var nodeLength = nodes.length;
        for (var i = 0; i < nodeLength; i++) {
            if (nodes[i].id == '0') {
                //根节点：展开
                treeObj.expandNode(nodes[i], true, true, false);
            } else {
                //非根节点：收起
                treeObj.expandNode(nodes[i], false, true, false);
            }
        }
    }
     
    /**
     * 搜索树，高亮显示并展示【模糊匹配搜索条件的节点s】
     * @param treeId
     * @param searchConditionId 文本框的id
     */
    function search_ztree(treeId, searchConditionId){
        searchByFlag_ztree(treeId, searchConditionId, "");
    }
     
    /**
     * 搜索树，高亮显示并展示【模糊匹配搜索条件的节点s】
     * @param treeId
     * @param searchConditionId     搜索条件Id
     * @param flag                  需要高亮显示的节点标识
     */
    function searchByFlag_ztree(treeId, searchConditionId, flag){
        //<1>.搜索条件
        var searchCondition = $('#' + searchConditionId).val();
        //解决ie9以下placeholder与value重复的问题
    	if(searchCondition == $('#' + searchConditionId).attr("placeholder")){
    		searchCondition = "";
    	}
        //<2>.得到模糊匹配搜索条件的节点数组集合
        var highlightNodes = new Array();
        if (searchCondition != "") {
            var treeObj = $.fn.zTree.getZTreeObj(treeId);
            highlightNodes = treeObj.getNodesByParamFuzzy("name", searchCondition, null);
        }
        //<3>.高亮显示并展示【指定节点s】
        highlightAndExpand_ztree(treeId, highlightNodes, flag);
        
        if (searchCondition == "") {
        	//展开所有
			zTree_Menu.expandAll(true);
        }
    }
     
    /**
     * 高亮显示并展示【指定节点s】
     * @param treeId
     * @param highlightNodes 需要高亮显示的节点数组
     * @param flag           需要高亮显示的节点标识
     */
    function highlightAndExpand_ztree(treeId, highlightNodes, flag){
        var treeObj = $.fn.zTree.getZTreeObj(treeId);
        //<1>. 先把全部节点更新为普通样式
        var treeNodes = treeObj.transformToArray(treeObj.getNodes());
        for (var i = 0; i < treeNodes.length; i++) {
            treeNodes[i].highlight = false;
            treeObj.updateNode(treeNodes[i]);
        }
        //<2>.收起树, 只展开根节点下的一级节点
        close_ztree(treeId);
        //<3>.把指定节点的样式更新为高亮显示，并展开
        if (highlightNodes != null) {
            for (var i = 0; i < highlightNodes.length; i++) {
                if (flag != null && flag != "") {
                    if (highlightNodes[i].flag == flag) {
                        //高亮显示节点，并展开
                        highlightNodes[i].highlight = true;
                        treeObj.updateNode(highlightNodes[i]);
                        //高亮显示节点的父节点的父节点....直到根节点，并展示
                        var parentNode = highlightNodes[i].getParentNode();
                        var parentNodes = getParentNodes_ztree(treeId, parentNode);
                        treeObj.expandNode(parentNodes, true, true, false);
                        treeObj.expandNode(parentNode, true, true, false);
                    }
                } else {
                    //高亮显示节点，并展开
                    highlightNodes[i].highlight = true;
                    treeObj.updateNode(highlightNodes[i]);
                    //高亮显示节点的父节点的父节点....直到根节点，并展示
                    var parentNode = highlightNodes[i].getParentNode();
                    var parentNodes = getParentNodes_ztree(treeId, parentNode);
                    treeObj.expandNode(parentNodes, true, true, false);
                    treeObj.expandNode(parentNode, true, true, false);
                }
            }
        }
    }
     
    /**
     * 递归得到指定节点的父节点的父节点....直到根节点
     */
    function getParentNodes_ztree(treeId, node){
        if (node != null) {
            var treeObj = $.fn.zTree.getZTreeObj(treeId);
            var parentNode = node.getParentNode();
            return getParentNodes_ztree(treeId, parentNode);
        } else {
            return node;
        }
    }
     
    /**
     * 设置树节点字体样式
     */
     function setFontCss_ztree(treeId, treeNode) {
    	 
     }
     
     function beforeDrag(treeId, treeNodes) {
			return false;
	 }
     
     function removeShop(fourth){
    	 var treeObj = $.fn.zTree.getZTreeObj("treeDemo");
    	 var node;
    	 node = treeObj.getNodeByParam("id", fourth, null);   
    	 
    	 if(node.chkDisabled){
    		 return;
    	 }
    	 
    	 //四级节点
    	 firstNode = node.getParentNode().getParentNode().getParentNode(); //一级节点
    	 secondNode = node.getParentNode().getParentNode();                //二级节点
    	 thirdNode = node.getParentNode();                                 //三级节点
    	 
    	 //判断是否有四级同级
    	 if(thirdNode.children.length > 1){
    		 //四级节点选中个数
    		 var checkedCount = 0;
    		 for(var i=0;i < thirdNode.children.length;i++){
    			 if(thirdNode.children[i].checked == true){
    				 checkedCount++;
    			 }
    		 }
    		//有四级已选中同级,只需要取消当前四级节点选中
    		 if(checkedCount > 1){
    			 node.checked = false;
    			 treeObj.updateNode(node);    			 
    		 }else{
    		//没有勾选的四级同级，判断三级是否有同级勾选
    			 var checkedCount = 0;
        		 for(var i=0;i < secondNode.children.length;i++){
        			 if(secondNode.children[i].checked == true){
        				 checkedCount++;
        			 }
        		 }
        		//没有勾选的四级同级，三级有同级勾选，只用取消当前三级及改三级的所有子节点的勾选
    			 if(checkedCount > 1){
    				 thirdNode.checked = false;
    				 treeObj.updateNode(thirdNode);
    				 for(var i=0;i < thirdNode.children.length;i++){
    					 thirdNode.children[i].checked = false;
    					 treeObj.updateNode(thirdNode.children[i]);
    				 }
    			 }else{
    			//没有勾选的四级同级，三级也没有同级勾选，判断二级是否有同级勾选	 
            		 var checkedCount = 0;
            		 for(var i=0;i < firstNode.children.length;i++){
            			 if(firstNode.children[i].checked == true){
            				 checkedCount++;
            			 }
            		 }
            		 
            		//没有勾选的四级同级，三级也没有同级勾选，判断二级有同级勾选，只需要取消当前二级节点及改节点的子节点	 
            		 if(checkedCount > 1){
            			 secondNode.checked = false;
            			 treeObj.updateNode(secondNode);
                		 for(var i=0;i < secondNode.children.length;i++){
                			 secondNode.children[i].checked = false;
                			 treeObj.updateNode(secondNode.children[i]);
                			 for(var j=0;j < secondNode.children[i].children.length;j++){
                				 secondNode.children[i].children[j].checked = false;
                				 treeObj.updateNode(secondNode.children[i].children[j]);
                			 }
                		 }
            		 }else{
            		//没有勾选的四级同级，三级也没有同级勾选，判断二级也没有同级勾选，取消该一级节点和其所有子节点
            			 firstNode.checked = false;
            			 treeObj.updateNode(firstNode);
                		 for(var i=0;i < firstNode.children.length;i++){
                			 firstNode.children[i].checked = false;
                			 treeObj.updateNode(firstNode.children[i]);
                			 for(var j=0;j < firstNode.children[i].children.length;j++){
                				 firstNode.children[i].children[j].checked = false;
                				 treeObj.updateNode(firstNode.children[i].children[j]);
                				 for(var k=0;k < firstNode.children[i].children[j].children.length;k++){
                					 firstNode.children[i].children[j].children[k].checked = false;
                    				 treeObj.updateNode(firstNode.children[i].children[j].children[k]);
                				 }
                			 }
                		 }
            		 }
    			 }
    		 }
    		 
    	 }else{
    		 //只有一个四级节点，判断是否有三级勾选同级
    		 if(secondNode.children.length > 1){
        		 var checkedCount = 0;
        		 for(var i=0;i < secondNode.children.length;i++){
        			 if(secondNode.children[i].checked == true){
        				 checkedCount++;
        			 }
        		 }
        		 
        		 //有三级同级勾选，只需取消当前三级和其所有子节点
        		 if(checkedCount > 1){
        			 thirdNode.checked = false;
    				 treeObj.updateNode(thirdNode);
    				 for(var i=0;i < thirdNode.children.length;i++){
    					 thirdNode.children[i].checked = false;
    					 treeObj.updateNode(thirdNode.children[i]);
    				 }
        		 }else{
        	   			//没有勾选的四级同级，三级也没有同级勾选，判断二级是否有同级勾选	 
            		 var checkedCount = 0;
            		 for(var i=0;i < firstNode.children.length;i++){
            			 if(firstNode.children[i].checked == true){
            				 checkedCount++;
            			 }
            		 }
            		 
            		//没有勾选的四级同级，三级也没有同级勾选，判断二级有同级勾选，只需要取消当前二级节点及改节点的子节点	 
            		 if(checkedCount > 1){
            			 secondNode.checked = false;
            			 treeObj.updateNode(secondNode);
                		 for(var i=0;i < secondNode.children.length;i++){
                			 secondNode.children[i].checked = false;
                			 treeObj.updateNode(secondNode.children[i]);
                			 for(var j=0;j < secondNode.children[i].children.length;j++){
                				 secondNode.children[i].children[j].checked = false;
                				 treeObj.updateNode(secondNode.children[i].children[j]);
                			 }
                		 }
            		 }else{
            		//没有勾选的四级同级，三级也没有同级勾选，判断二级也没有同级勾选，取消该一级节点和其所有子节点
            			 firstNode.checked = false;
            			 treeObj.updateNode(firstNode);
                		 for(var i=0;i < firstNode.children.length;i++){
                			 firstNode.children[i].checked = false;
                			 treeObj.updateNode(firstNode.children[i]);
                			 for(var j=0;j < firstNode.children[i].children.length;j++){
                				 firstNode.children[i].children[j].checked = false;
                				 treeObj.updateNode(firstNode.children[i].children[j]);
                				 for(var k=0;k < firstNode.children[i].children[j].children.length;k++){
                					 firstNode.children[i].children[j].children[k].checked = false;
                    				 treeObj.updateNode(firstNode.children[i].children[j].children[k]);
                				 }
                			 }
                		 }
            		 }
        		 }
    		 }else{
    			//只有一个三级节点，判断是否有二级同级勾选
    			 if(firstNode.children.length > 1){
	        		 var checkedCount = 0;
	        		 for(var i=0;i < firstNode.children.length;i++){
	        			 if(firstNode.children[i].checked == true){
	        				 checkedCount++;
	        			 }
	        		 }
	        		 
	        		//没有勾选的四级同级，三级也没有同级勾选，判断二级有同级勾选，只需要取消当前二级节点及改节点的子节点	 
	        		 if(checkedCount > 1){
	        			 secondNode.checked = false;
	        			 treeObj.updateNode(secondNode);
	            		 for(var i=0;i < secondNode.children.length;i++){
	            			 secondNode.children[i].checked = false;
	            			 treeObj.updateNode(secondNode.children[i]);
	            			 for(var j=0;j < secondNode.children[i].children.length;j++){
	            				 secondNode.children[i].children[j].checked = false;
	            				 treeObj.updateNode(secondNode.children[i].children[j]);
	            			 }
	            		 }
	        		 }else{
	        		//没有勾选的四级同级，三级也没有同级勾选，判断二级也没有同级勾选，取消该一级节点和其所有子节点
	        			 firstNode.checked = false;
	        			 treeObj.updateNode(firstNode);
	            		 for(var i=0;i < firstNode.children.length;i++){
	            			 firstNode.children[i].checked = false;
	            			 treeObj.updateNode(firstNode.children[i]);
	            			 for(var j=0;j < firstNode.children[i].children.length;j++){
	            				 firstNode.children[i].children[j].checked = false;
	            				 treeObj.updateNode(firstNode.children[i].children[j]);
	            				 for(var k=0;k < firstNode.children[i].children[j].children.length;k++){
	            					 firstNode.children[i].children[j].children[k].checked = false;
	                				 treeObj.updateNode(firstNode.children[i].children[j].children[k]);
	            				 }
	            			 }
	            		 }
	        		 }
	    		 }else{
	    		//只有一个二级节点
	    			 
	    			 node.checked = false;
					 treeObj.updateNode(node);
					 thirdNode.checked = false;
					 treeObj.updateNode(thirdNode);
					 secondNode.checked = false;
					 treeObj.updateNode(secondNode);
					 firstNode.checked = false;
					 treeObj.updateNode(firstNode);
	    		 }
    		 }	 
    	 }
  
    	 $("#shopId" + fourth).remove();
    }
     
     function filter(node) {
    	    return (node.level == 3 && node.checked == true);
    	}